package test;

public class produce implements Runnable{
	 Thread t;
	 String name;
	 private srcs obj;
	 /**
	  * obj from main,so different thread can share the same obj.
	  * if obj=new Object() here in thread_sy class,each thread will create a new obj,so sychronize
	  * is invalid
	  */
	 
	 public produce(String name,srcs obj){
		 this.name=name;
		 this.obj=obj;
		 
	 }
	 public  void run(){
		 System.out.println("producer start"+obj.data);
		/* while(true){
			 obj.data+=1;
			 try{
				 t.sleep(10);
				 }catch(Exception e){
					 e.printStackTrace();
				 }
			 System.out.println("P:"+obj.data);
			 }*/
            while(true){
			 synchronized (obj) {
				 while(obj.data>0){
					try{		
						System.out.println("producer begin wait,obj is :"+obj.data);
						 obj.wait();
					   }catch( Exception e){
						 e.printStackTrace();
					   }
				     }
					
					obj.data++;
					System.out.println("producer produce one,obj is :"+obj.data);
					obj.notify();
				
			 }
          }
	 }
	 public void start(){
		 if(t==null){
			 t=new Thread(this,this.name);
			 t.start();
		 }
	 }
}